<template>
	<view class="timebar flex align-center">
		<u-icon name="arrow-left" size="36" color="#000" @tap="prevTime"></u-icon>
		<zxz-uni-datetime-picker :type="timeType" returnType="yyyy-WW" placeholder="选择周" :clear-icon="true" v-model="yearWeek" isMonday @change="changeTime">
			<view class="text-center">{{ yearWeek }}</view>
		</zxz-uni-datetime-picker>
		<u-icon name="arrow-right" size="36" color="#000" @tap="nextTime"></u-icon>
	</view>
</template>

<script>
import { week_date, getYearWeek, addZero } from '@/uni_modules/zxz-uni-datetime-picker/components/zxz-uni-datetime-picker/zxz-util.js';
export default {
	name: 'timebar',
	props: {
		timeType: {
			type: String,
			default: 'day'
		}
	},
	data() {
		return {
			startDay: '',
			endDay: '',
			yearWeek: ''
		};
	},
	watch: {
		timeType(newVal, oldVal) {
			console.log('timeType', this.timeType);
			if (this.timeType == 'week') {
				let week = getYearWeek(new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate());
				this.yearWeek = new Date().getFullYear() + '-' + week;
				this.changeTime(this.yearWeek);
			} else if (this.timeType == 'yearMonth') {
				this.yearWeek = new Date().getFullYear() + '-' + addZero(new Date().getMonth() + 1);
				this.changeTime(this.yearWeek);
			} else if (this.timeType == 'date') {
				let date = new Date();
				const year = date.getFullYear();
				const month = date.getMonth() + 1;
				const day = date.getDate();
				this.yearWeek = `${year}-${addZero(month)}-${addZero(day)}`;
				this.changeTime(this.yearWeek);
			} else {
				this.yearWeek = new Date().getFullYear();
				this.changeTime(this.yearWeek);
			}
		}
	},
	created() {
		let date = new Date();
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const day = date.getDate();
		this.yearWeek = `${year}-${addZero(month)}-${addZero(day)}`;
		this.changeTime(this.yearWeek, 1);
	},
	methods: {
		prevTime() {
			if (this.timeType == 'year') {
				this.yearWeek--;
				this.changeTime(this.yearWeek);
			} else if (this.timeType == 'date') {
				var curDate = new Date(this.yearWeek);
				var date = new Date(curDate.getTime() - 24 * 60 * 60 * 1000);
				var year = date.getFullYear(), //获取完整的年份(4位)
					month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
					strDate = date.getDate(); // 获取当前日(1-31)
				if (month < 10) month = `0${month}`; // 如果月份是个位数，在前面补0
				if (strDate < 10) strDate = `0${strDate}`; // 如果日是个位数，在前面补0

				this.yearWeek = `${year}-${month}-${strDate}`; //如果只需要月、日 return值为 `${month}-${strDate}`
				this.changeTime(this.yearWeek);
			} else {
				let nums = this.yearWeek.split('-');
				if (this.timeType == 'week') {
					let week = nums[1] - 1;
					let yearWeek = nums[0] + '-' + week;
					this.changeTime(yearWeek);
					let s = this.startDay.split('-');
					week = getYearWeek(s[0], s[1], s[2]);
					this.yearWeek = s[0] + '-' + week;
				} else {
					let year = nums[0];
					let month = nums[1] - 1;
					if (month <= 0) {
						month = 12;
						year--;
					}
					this.yearWeek = year + '-' + addZero(month);
					this.changeTime(this.yearWeek);
				}
			}
		},
		nextTime() {
			if (this.timeType == 'year') {
				this.yearWeek++;
				this.changeTime(this.yearWeek);
			} else if (this.timeType == 'date') {
				var curDate = new Date(this.yearWeek);
				var date = new Date(curDate.getTime() + 24 * 60 * 60 * 1000);
				var year = date.getFullYear(), //获取完整的年份(4位)
					month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
					strDate = date.getDate(); // 获取当前日(1-31)
				if (month < 10) month = `0${month}`; // 如果月份是个位数，在前面补0
				if (strDate < 10) strDate = `0${strDate}`; // 如果日是个位数，在前面补0

				this.yearWeek = `${year}-${month}-${strDate}`; //如果只需要月、日 return值为 `${month}-${strDate}`
				this.changeTime(this.yearWeek);
			} else {
				let nums = this.yearWeek.split('-');
				let year = nums[0];
				let month = nums[1] - 0 + 1;
				if (month > 12) {
					month = 1;
					year++;
				}
				this.yearWeek = year + '-' + addZero(month);
				this.changeTime(this.yearWeek);
			}
		},
		//type 是否需要刷新数据 1不需要 0需要
		changeTime(e, type) {
			console.log('e', e, this.timeType);
			if (this.timeType == 'week') {
				let nums = e.split('-');
				if (nums[0] != 'undefined' && nums[1] != 'undefined' && /^\d{4}$/.test(nums[0])) {
					let week = week_date(e);
					console.log('week', week);
					this.startDay = week[0];
					this.endDay = week[1];
				}
			} else if (this.timeType == 'year') {
				this.startDay = e + '-01-01';
				this.endDay = e + '-12-31';
			} else {
				let nums = this.yearWeek.split('-');
				if (this.timeType == 'date') {
					this.startDay = e;
					this.endDay = this.startDay;
				} else {
					this.startDay = e + '-01';
					this.endDay = e + '-' + new Date(nums[0], nums[1], 0).getDate();
				}
			}
			uni.$u.debounce(
				this.$emit('updateData', {
					startDay: this.startDay,
					endDay: this.endDay
				}),
				500
			);
		}
	}
};
</script>

<style scoped lang="scss">
.timebar {
	background: #ececec;
	border-radius: 16upx;
	color: #000;
	font-size: 34upx;
	padding: 10upx;
}
</style>
